<?php

/*	Vulpes-backend - Web-based managegent for wireless internet providers.
	Copyright (C) 2007 LinFox Serviços em Informática Ltda.

	This file is part of the Vulpes backend.

	Vulpes-backend is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Vulpes-backend is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 	Please note that any front-end (client application) that is distributed
	along with the Vulpes back-end, such as the Vulpes front-end by LinFox,
	is subject to it's correspending licene. */

require_once 'dao.php';
require_once 'contract.php';

class contract_dao extends dao
{
	public $key = 'handle';
	protected $model = 'contract';
	protected $table = 'contracts';

	function __construct()
	{
		parent::__construct();
	}
	
	function fetch($handle = false)
	{
		if ($handle) {
			$contract_sql = '
				SELECT `contracts`.*,
					`clients`.`username` as `client_username`
				FROM `'.$this->table.'`
				INNER JOIN `clients` ON (
					`clients`.`client_id` = `contracts`.`client_id` )
				WHERE `handle` = "'.$handle.'"';
			$contract_result = $this->db->query($contract_sql);
			if (!$contract = $contract_result->fetchObject('contract')) {
				throw new Exception('contract_not_found');
			}
			$contract->ping = $contract->ping();
			return $contract;
		}
	}

	function fetch_client($client_id)
	{
		$contracts = array();
		$sql_contracts = '
			SELECT
				`contracts`.*,
				`clients`.`username` AS `client_username`,
				`plans`.`name` AS `plan_name`
			FROM `contracts`
			LEFT JOIN `clients` ON (
				`clients`.`client_id` = `contracts`.`client_id` )
			LEFT JOIN `plans` ON (
				`plans`.`plan_id` = `contracts`.`plan_id` )
			WHERE `clients`.`client_id` = "'.$client_id.'"';
		$result_contracts = $this->db->query($sql_contracts);
		while ($contract = $result_contracts->fetchObject($this->model)) {
			$contracts[] = $contract;
		}
		return $contracts;
	}
	
	function fetch_all($search = false)
	{
		$contracts = array();
		$sql_contracts = '
			SELECT
				`contracts`.*,
				`clients`.`username` AS `client_username`,
				`clients`.`active` AS `active`,
				`plans`.`name` AS `plan_name`
			FROM `contracts`
			LEFT JOIN `clients` ON (
				`clients`.`client_id` = `contracts`.`client_id` )
			LEFT JOIN `plans` ON (
				`plans`.`plan_id` = `contracts`.`plan_id` )';
		if ($search) {
			$sql_contracts .= '
				WHERE `clients`.`name` LIKE "%'.$search.'%"
				OR `clients`.`username` LIKE "%'.$search.'%"
				OR `contracts`.`ip` LIKE "%'.$search.'%"';
		}
		$result_contracts = $this->db->query($sql_contracts);
		while ($contract = $result_contracts->fetchObject('contract')) {
			$contracts[] = $contract;
		}
		return $contracts;
	}

}

?>
